$(function () {
    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var all_datagrid =  $('#all_datagrid');

    //数据表格
    all_datagrid.datagrid({
        url:'/exam/list.do',
        title:"考试管理管理",
        striped:true,
        singleSelect:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'client1',title:'客户姓名',width:100,formatter:nameFormatter},
            {field:'client2',title:'QQ',width:100,formatter:qqFormatter},
            {field:'tel',title:'电话',width:100,formatter:telFormatter},
            {field:'clazz',title:'意向班级',width:100,formatter:clazzFormatter},
            {field:'examType',title:'考试类型',width:100,formatter:examTypeFormatter},
            {field:'examTime',title:'考试时间时间',width:100},
            {field:'client3',title:'营销人员',width:100,formatter:marketerFormatter},
            {field:'client4',title:'录入人员',width:100,formatter:inputerFormatter},
            {field:'result',title:'考试结果',width:100,formatter:resultFormatter},
            {field:'remark',title:'备注',width:100},
            {field:'handler',title:'处理人',width:100,formatter:handlerFormatter}
        ]]
    });
    //end



    var cmdObj = {
        //审核
        audit:function(){
            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要审核的客户","info");
                return;
            }

            if (row.result){
                $.messager.alert("温馨提示","该用户已经审核","info");
                return;
            }


            $.messager.confirm('确认','确认要进行审核操作吗吗？',function(r){
                if (r){
                    $.get("/exam/audit.do?id="+row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','审核完成',function(r){
                                all_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            })
        },

        //重置
        clear:function(){
            edit_form.form("clear");
        },

        query : function () {
            var param = $("#keyword").val();
            var beginTime = $("#beginTime").val();
            var endTime = $("#endTime").val();

            $('#all_datagrid').datagrid('load',{
                keyword: param,
                beginTime: beginTime,
                endTime: endTime
            });
        },

        //点击添加按钮,添加考试管理
        add : function () {
            edit_dialog.dialog("setTitle","添加考试管理");
            edit_form.form("clear");

            $('.dialog-button a:eq(0)').show();
            $('.dialog-button a:eq(1)').show();
            edit_dialog.dialog("open");
        },
        //刷新
        reload : function () {
            all_datagrid.datagrid("reload");
        },
        //编辑按钮编辑考试管理
        edit : function () {

            //显示按钮
            $('.dialog-button a:eq(0)').show();
            $('.dialog-button a:eq(1)').show();

            var row = all_datagrid.datagrid("getSelected");
            console.log(row);
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的考试管理","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑考试管理");

            //在读取数据之前,清除缓存
            edit_form.form("clear");




            if(row.clazz){
                row["clazz.id"]=row.clazz.id;
            }
            if(row.client){
                row["client.id"]=row.client.id;
            }
            if(row.examType){
                row["examType.id"]=row.examType.id;
            }

            edit_form.form("load",row);

            edit_dialog.dialog("open");
        },
        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#hiddenId").val();

            if(id){
                url = "/exam/update.do";
            }else{
                url = "/exam/save.do";
            }

            edit_form.form('submit', {
                url:url,
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            all_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //删除考试管理
        remove : function () {
            var row = all_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的考试管理","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/exam/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                all_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },

        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        }
    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end

    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: "60%",
        height: "80%"
    });
    //end

})//end




function nameFormatter(value,row,index) {

    return row.client ? row.client.name : "";
}


function qqFormatter(value,row,index) {

    return row.client ? row.client.qqNum : "";
}

function telFormatter(value,row,index) {

    return row.client ? row.client.tel : "";
}
function clazzFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.name;
}

function examTypeFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.name;
}

function marketerFormatter(value,row,index) {

    return row.client ? row.client.marketer.username : "" ;
}

function inputerFormatter(value,row,index) {

    return row.client ? row.client.inputer.username : "";
}
function handlerFormatter(value,row,index) {

    if (value == null){
        return;
    }
    return value.username;
}
function resultFormatter(value,row,index) {

    if (value){
        return "<span style='color: green'>已通过</span>";
    }else{
        return "<span style='color: red'>未通过</span>";
    }
}
